package com.sls.nzdapplication.db.utils;

import android.content.Context;

import com.alibaba.fastjson.JSONArray;
import com.sls.nzdapplication.app.MySharedPreferences;
import com.sls.nzdapplication.db.DaoManager;
import com.sls.nzdapplication.db.DaoSession;
import com.sls.nzdapplication.db.MeterBoxDao;
import com.sls.nzdapplication.model.MeterBox;
import com.sls.nzdapplication.app.MySharedPreferences;


import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MeterBoxUtil {
    private static final boolean DUBUG = true;
    private DaoManager manager;
    private MeterBox meterBox;
    private DaoSession daoSession;
    private MySharedPreferences mySharedPreferences;
    private String username = "";

    public MeterBoxUtil(Context context){
        manager = DaoManager.getInstance();
        manager.init(context);
        daoSession = manager.getDaoSession();
        manager.setDebug(DUBUG);
        mySharedPreferences = new MySharedPreferences(context);
        username = mySharedPreferences.getUserValue("username");
    }

    /**
     * 添加数据，如果有重复则覆盖
     */
    public void insertMeterBox(MeterBox meterBox) {
        meterBox.setCreateBy(username);
        meterBox.setCreateTime(new Date());
        manager.getDaoSession().insertOrReplace(meterBox);
    }


    /**
     * 添加多条数据，需要开辟新的线程
     */
    public void insertMultMeterBox(final List<MeterBox> meterBoxs) {
        manager.getDaoSession().runInTx(new Runnable() {
            @Override
            public void run() {
                for (MeterBox meterBox : meterBoxs) {
                    meterBox.setCreateBy(username);
                    meterBox.setCreateTime(new Date());
                    manager.getDaoSession().insertOrReplace(meterBox);
                }
            }
        });
    }

    /**
     * 添加多条数据，需要开辟新的线程
     */
    public void insertMultMeterBox2(final List<MeterBox> meterBoxs) {
        manager.getDaoSession().runInTx(new Runnable() {
            @Override
            public void run() {
                for (MeterBox meterBox : meterBoxs) {
                    manager.getDaoSession().insertOrReplace(meterBox);
                }
            }
        });
    }

    /**
     * 删除数据
     */
    public void deleteMeterBox(MeterBox meterBox) {
        manager.getDaoSession().delete(meterBox);
    }

    /**
     * 删除全部数据
     */
    public void deleteAll(Class cls) {
        manager.getDaoSession().deleteAll(cls);
    }

    /**
     * 更新数据
     */
    public void updateMeterBox(MeterBox meterBox) {
        meterBox.setModifyBy(username);
        meterBox.setModifyTime(new Date());
        manager.getDaoSession().update(meterBox);
    }

    /**
     * 按照主键返回单条数据
     */
    public MeterBox listOneMeterBox(String key) {
        return manager.getDaoSession().load(MeterBox.class, key);
    }

    public List<MeterBox> queryMeterBoxByLineId(String id){
        QueryBuilder<MeterBox> builder = manager.getDaoSession().queryBuilder(MeterBox.class);
        List<MeterBox> meterBoxList = builder.where(MeterBoxDao.Properties.AreaId.eq(id)).list();
        return  meterBoxList;
    }

    public List<MeterBox> queryMeterBoxByLineIdTime(String id){
        QueryBuilder<MeterBox> builder = manager.getDaoSession().queryBuilder(MeterBox.class);
        List<MeterBox> meterBoxList = builder.where(MeterBoxDao.Properties.AreaId.eq(id)).orderDesc(MeterBoxDao.Properties.CreateTime).list();
        return  meterBoxList;
    }

    public List<MeterBox> queryMeterBoxByLineIdTime2(String id){
        QueryBuilder<MeterBox> builder = manager.getDaoSession().queryBuilder(MeterBox.class);
        List<MeterBox> meterBoxList = builder.where(MeterBoxDao.Properties.AreaId.eq(id)).orderAsc(MeterBoxDao.Properties.CreateTime).list();
        return  meterBoxList;
    }

    public List<MeterBox> queryMeterBoxByLineIdAndCode(String id,String code){
        QueryBuilder<MeterBox> builder = manager.getDaoSession().queryBuilder(MeterBox.class);
        List<MeterBox> meterBoxList = builder.where(builder.and(MeterBoxDao.Properties.AreaId.eq(id),MeterBoxDao.Properties.BoxCode.eq(code))).list();
        return  meterBoxList;
    }

    public List<MeterBox> queryMeterBoxByLineIdAndName(String id,String code){
        QueryBuilder<MeterBox> builder = manager.getDaoSession().queryBuilder(MeterBox.class);
        List<MeterBox> meterBoxList = builder.where(builder.and(MeterBoxDao.Properties.AreaId.eq(id),MeterBoxDao.Properties.BoxName.eq(code))).list();
        return  meterBoxList;
    }



    /**
     * 查询全部数据
     */
    public List<MeterBox> queryAll() {
        return manager.getDaoSession().loadAll(MeterBox.class);
    }

    public JSONArray queryAllToJson(){
        JSONArray jsonArray = new JSONArray();
        List<MeterBox> meterBoxs = queryAll();
        for(MeterBox meterBox :meterBoxs){
            jsonArray.add(meterBox.toJson());
        }
        return jsonArray;
    }

    public void insertByJsArray(JSONArray jsonArray){
        List<MeterBox> list = new ArrayList<>();
        for(int i = 0;i<jsonArray.size();i++){
            list.add(new MeterBox(jsonArray.getJSONObject(i)));
        }
        insertMultMeterBox2(list);
    }
}
